package com.zs.zhiling.controller;

import com.zs.zhiling.model.User;
import com.zs.zhiling.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@Api(value = "users",description = "用户管理功能")
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    //得到所用的用户
    @ApiOperation(value = "查询所有用户",notes = "查询所有用户")
    @GetMapping("/findAllUsers")
    public List<User> getAllUsers(){
        return userService.findAllUsers();
    }
    //删除用户
    @ApiOperation(value = "删除一个用户信息",notes = "删除一个用户信息")
    @ApiImplicitParam(name = "userid",value = "用户id",required = true,dataType = "int")
    @GetMapping("/deleteUserById")
    public String deleteUserById(Integer userid){
       int n= userService.removeUserById(userid);
       String msg=n>0?"删除成功":"删除失败";
       return msg;
    }
    //修改用户
    @GetMapping("/updateUser")
    @ApiOperation(value = "根据用户id修改用户",notes = "根据用户id修改用户")
    @ApiImplicitParams({@ApiImplicitParam(name = "uid",value = "用户id",required = true,dataType = "int"),
    @ApiImplicitParam(name = "username",value = "用户姓名",required = true,dataType = "String"),
    @ApiImplicitParam(name = "userpwd",value = "用户密码",required = true,dataType = "String")})
    public String updateAndAddUser(@RequestParam("uid") int uid,@RequestParam("username") String username,
                                   @RequestParam("userpwd") String userpwd){
        User user=new User(uid,username,userpwd);
        String msg=null;
            int n=userService.updateUserById(user);
            msg=n>0?"修改成功":"修改失败";
        return msg;
    }
    //添加用户
    @ApiOperation(value = "添加一个用户",notes = "添加一个用户")
    @ApiImplicitParam(name = "user",value = "用户对象",required = true,dataType = "User")
    @GetMapping("/addUser")
    public String saveUser(User user){
        String msg=null;
        int n= userService.addUser(user);
        msg=n>0?"添加成功":"添加失败";
        return msg;
    }

    //根据用户id查找用户
    @GetMapping("findUserById")
    @ApiOperation(value = "根据用户id获得用户",notes = "根据用户id获得用户")
    @ApiImplicitParam(name = "userid",value = "用户Id",required = true,dataType = "int")
    public User findUserById(Integer userid){
        return userService.findUserById(userid);
    }
}
